package com.kx.mybatis.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.kx.mybatis.pojo.MemberUser;
import com.kx.mybatis.service.UserService;
import com.kx.mybatis.service.impl.UserServiceImpl;
import com.kx.mybatis.state.R;
import com.kx.mybatis.state.ResultCode;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @Description
 * @ClassName UserServlet
 * @Author Sun
 * @Date 2021/5/15 18:01
 * @Version 1.0
 */
@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String option = req.getParameter("option");
        if (option == null || "".equals(option.trim())) {
            resp.setContentType("application/text;charset=utf-8");
            R r = R.setError(ResultCode.NOOPTION, "没有传递option参数");
            String s = JSON.toJSONString(r);
            resp.getWriter().write(s);
        }
        switch (option) {
            case "query":
                query(req, resp);
                break;
            case "list":
                list(req, resp);
                break;
            case "add":
                add(req, resp);
        }

    }

    private void query(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/text;charset=utf-8");
        String id = req.getParameter("id");
        MemberUser user = userService.getUserById(id);
        R r = null;
        if(user==null){
            r = R.setError(ResultCode.USER_ID_ERROR,"用户不存在");
        }else {
            r = R.setOk(user);
        }

        //通过json形式数据响应给页面
        String json = JSON.toJSONString(r);
        resp.getWriter().write(json);

    }

    //分页查询
    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        //分页需要知道 1.当前第几页 2.每页显示多少条数据
        String pageIndex = req.getParameter("pageIndex");
        String pageSize = req.getParameter("pageSize");
        //对不合规的数据设默认值
        if(pageIndex==null||"".equals(pageIndex.trim())){
            pageIndex = "1";
        }
        if(pageSize==null||"".equals(pageSize.trim())){
            pageSize = "10";
        }
        PageInfo<MemberUser> pageInfo = userService.findAllUsers(pageIndex,pageSize);
        R r = R.setOk(pageInfo);
        String json = JSON.toJSONString(r);
        resp.getWriter().write(json);

    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        resp.setContentType("application/json;charset=utf-8");
        //通过输入流读取前台发来的json数据
        R r = null;
        try {
            StringBuffer buffer = new StringBuffer();
            ServletInputStream is = req.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is,"utf-8"));
            String line = null;
            while ((line = br.readLine()) != null) {
                buffer.append(line);
            }
            MemberUser user = JSON.parseObject(buffer.toString(), MemberUser.class);
            userService.addUser(user);
            r=R.setOk();
        } catch (Exception e){
            e.printStackTrace();
            r=R.setError(ResultCode.USER_ADD_ERROR, "数据格式不正确");
        }
        String s = JSON.toJSONString(r);
        resp.getWriter().write(s);

    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
}
